//
//  ViewController.m
//  FileDemo
//
//  Created by 徐小龙 on 2019/6/13.
//  Copyright © 2019 徐小龙. All rights reserved.
//

#import "ViewController.h"
#import "IntoDocumentsManager.h"
#import "DocumentListCell.h"
#import "DocumentModel.h"

@interface ViewController ()
<
UITableViewDelegate,
UITableViewDataSource
>
@property (weak, nonatomic) IBOutlet UITableView *mainTableView;
@property (nonatomic, strong) NSMutableArray <DocumentModel *> *documentsArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"%@",NSHomeDirectory());
    
    [self initDocumentsList];
    
    [self.mainTableView registerNib:[UINib nibWithNibName:@"DocumentListCell" bundle:nil]
             forCellReuseIdentifier:@"DocumentListCellIdentifier"];
    // Do any additional setup after loading the view.
}

- (void)initDocumentsList{
    
    if (self.documentsArray == nil) {
        self.documentsArray = [NSMutableArray array];
    }
    
    [self.documentsArray removeAllObjects];
    NSArray *array = [IntoDocumentsManager GetAllIntoDocuments];
    [self.documentsArray  addObjectsFromArray:array];
    
    for (DocumentModel *model in self.documentsArray) {
        NSLog(@"%@",model);
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.documentsArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DocumentListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DocumentListCellIdentifier"];
    if (cell == nil) {
        [tableView registerNib:[UINib nibWithNibName:@"DocumentListCell" bundle:nil]
        forCellReuseIdentifier:@"DocumentListCellIdentifier"];
        cell = [tableView dequeueReusableCellWithIdentifier:@"DocumentListCellIdentifier"];
    }
    DocumentModel *model = self.documentsArray[indexPath.row];
    [cell configCellWith:model];
    return cell;
}

@end
